package cn.icare.cms.controller.restful;

import cn.icare.cms.StoreService;
import cn.icare.cms.model.ro.AddStoreRO;
import cn.icare.cms.model.ro.EditStoreRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/store")
@Api(tags = "REST仓库")
public class StoreController extends BaseController {
    @Resource
    private StoreService storeService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/store
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个仓库", notes = "t_AD表新增单个仓库")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddStoreRO ro, HttpServletRequest request) {

        //仓库防止重复检查
        boolean isExists= this.storeService.isExists(0,ro.getAreaId(),ro.getName());
        if(isExists)
        {
            return super.getRespFail("仓库名称已经存在，不能重复添加");
        }

        String userName=this.getCurrentUserName(request);
        Integer id = this.storeService.createOne(ro,userName);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/store
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个仓库", notes = "更新单个仓库")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditStoreRO ro,HttpServletRequest request) {

        //仓库防止重复检查
        boolean isExists= this.storeService.isExists(ro.getId(),ro.getAreaId(),ro.getName());
        if(isExists)
        {
            return super.getRespFail("仓库名称已经存在，不能重复添加");
        }
        String userName=this.getCurrentUserName(request);
        if (this.storeService.updateOne(ro,userName)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/store/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个仓库", notes = "删除符合主键的单个仓库")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.storeService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/store?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个仓库", notes = "删除符合主键的多个仓库")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.storeService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/store/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个仓库", notes = "返回符合主键的单个仓库")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.storeService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/store?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个仓库", notes = "user表返回符合条件的多个仓库")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "areaId", required = false) Integer areaId,
                                             @RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize
    ) {
        return super.getRespOk(this.storeService.getList(areaId,name,pageNum,pageSize));
    }



    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddStoreRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditStoreRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Integer areaId,String name,Integer pageNum,Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
